package com.zxpt.quetion.service.impl;

import com.zxpt.quetion.mappervo.QuestionMouldMapperVo;
import com.zxpt.quetion.pojovo.QuUserQuesVo;
import com.zxpt.quetion.pojovo.QuestionMouldVo;
import com.zxpt.quetion.service.IQuUserQuesService;
import com.zxpt.quetion.mappervo.QuUserQuesMapperVo;
import com.zxpt.quetion.service.IQuestionMouldService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import  com.zxpt.quetion.utils.Query;
import  com.zxpt.quetion.utils.CreateQueryConditionUtil;
import  com.zxpt.quetion.utils.SqlUtil;

/**
* 所在包：com.zxpt.quetion.service.impl.QuUserQuesServiceImpl
* 文件名：QuUserQuesServiceImpl
* 版权：稻草人.
* 描述：
* 修改人：rmk
* 修改时间：
*/
@Service
@Transactional
@Slf4j
public class QuUserQuesServiceImpl implements IQuUserQuesService {

    @Resource
    private QuUserQuesMapperVo quUserQuesMapperVo;
    @Autowired
    private IQuestionMouldService questionMouldService;

    @Override
    public PageInfo<QuUserQuesVo> getPage (Map<String, Object> queryMap, Integer page, Integer rows) {
        Query query = CreateQueryConditionUtil.createCondition(queryMap);
        PageHelper.startPage(page, rows);
        List<QuUserQuesVo> list = quUserQuesMapperVo.getPage(query.getSql(), queryMap);
        PageInfo<QuUserQuesVo> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<QuUserQuesVo> getGroup(Map<String, Object> queryMap) {
        Query query = CreateQueryConditionUtil.createCondition(queryMap);
        List<QuUserQuesVo> list = quUserQuesMapperVo.getGroup(query.getSql(), queryMap);
        return list;
    }

    @Override
    public Map<String, Integer> getCountValue(Map<String, Object> map) {
        Query query = CreateQueryConditionUtil.createCondition(map);
        List<QuUserQuesVo> list = quUserQuesMapperVo.getPage(query.getSql(), map);
        final String[] allOp_c = {""};
        list.forEach(x ->{
            allOp_c[0] = allOp_c[0] +x.getOptions()+",";
        });
        String allOp = allOp_c[0];
        String[] allOp_arr = allOp.split(",");
        Map<String,Integer> re_map = new HashMap<>();
        for(int i = 0;i<allOp_arr.length;i++){
            String opid = allOp_arr[i];
            if(re_map.containsKey(opid)){
                re_map.put(opid,re_map.get(opid)+1);
            }else{
                re_map.put(opid,1);
            }
        }
        return re_map;
    }

    @Override
    public QuUserQuesVo getById(String id) {
        QuUserQuesVo quUserQuesVo = quUserQuesMapperVo.getById(id);
        return quUserQuesVo;
    }

    @Override
    public void addOrUpdate(List<QuUserQuesVo> userQues_list) {
        boolean newRecovery = false;
        //回答是否回收率+1
        QuUserQuesVo test_Mould_recovery = userQues_list.get(0);
        String test_Mould_id =test_Mould_recovery.getMouldId();
        String test_user  = test_Mould_recovery.getUid();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("mouldId__equal",test_Mould_id);
        queryMap.put("uid__equal",test_user);
        Query query = CreateQueryConditionUtil.createCondition(queryMap);
        List<QuUserQuesVo> list = quUserQuesMapperVo.getPage(query.getSql(), queryMap);
        if(CollectionUtils.isEmpty(list)){
            newRecovery=true;
        }
        List<QuUserQuesVo> inset_answers= userQues_list.stream().filter(x -> x.getId() == null).collect(Collectors.toList());
        List<QuUserQuesVo> update_answers= userQues_list.stream().filter(x -> x.getId() != null).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(inset_answers)){
            inset_answers.forEach(this::add);
        }
        if(CollectionUtils.isNotEmpty(update_answers)){
            update_answers.forEach(this::update);
        }
        //所有回答保存成功后
        if(newRecovery){
            QuestionMouldVo questionMouldVo = questionMouldService.getById(test_Mould_id);
            if(questionMouldVo.getRecovery() == null){
                questionMouldVo.setRecovery(1);
            }else{
                questionMouldVo.setRecovery(questionMouldVo.getRecovery()+1);
            }
            questionMouldService.update(questionMouldVo);
        }
    }

    @Override
    public void add(QuUserQuesVo quUserQuesVo) {
        SqlUtil<QuUserQuesVo> sqlUtil = new SqlUtil<>();
        String sql = sqlUtil.createInsertSql(QuUserQuesVo.class, quUserQuesVo);
        quUserQuesMapperVo.insertSelective(quUserQuesVo, sql);
    }

    @Override
    public void update(QuUserQuesVo quUserQuesVo) {
        SqlUtil<QuUserQuesVo> sqlUtil = new SqlUtil<>();
        String sql = sqlUtil.createUpdateSelectiveSql(QuUserQuesVo.class, quUserQuesVo);
        quUserQuesMapperVo.updateSelective(quUserQuesVo, sql);
    }

    @Override
    public void updateAbsolute(QuUserQuesVo quUserQuesVo) {
        SqlUtil<QuUserQuesVo> sqlUtil = new SqlUtil<>();
        String sql = sqlUtil.createUpdateAbsoluteSql(QuUserQuesVo.class);
        quUserQuesMapperVo.updateAbsolute(quUserQuesVo, sql);
    }

    @Override
    public void delById(String id) {
        quUserQuesMapperVo.delById(id);
    }
}
